package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import vo.Base;
import vo.Product;
import dao.ProductDao;
@Repository
public class ProductDaoImpl implements ProductDao {
	@PersistenceContext(name="un")
	private EntityManager em;
	@Override
	public String addProduct(Product p) {
		em.getReference(Base.class, p.getBase().getId());
		em.persist(p);;
		return p.getId();
	}

	@Override
	public int deleteProduct(String id) {
		Product p=em.getReference(Product.class, id);
		em.remove(p);
		return 1;
	}

	@Override
	public List<Product> findAllProducts() {
		String jpql="from Product";
		
		return em.createQuery(jpql).getResultList();
	}

	@Override
	public List<Product> findProductsByBaseId(String id) {
		String jqpl="from Product p where p.base.id=:id";
		return em.createQuery(jqpl).setParameter("id", id).getResultList();
	}

}






